//获取应用实例
const app = getApp()
Page({
  data: {
    autoplay: true,
    interval: 3000,
    duration: 1000,
    activeIndex: 0,
    // 项目详情,
    // min
    min: 2,
    show: false,
  },
  onLoad: function (options) {
    if (app.globalData.user) {
      let uid = app.globalData.user.id
      let id = JSON.parse(options.id)
      this._getInfo({ id, uid })
      this.setData({ uid })
    }
  },
  onShow() {
    if (app.globalData.user && app.globalData.user.id) {
    } else {
      this._getAuthority()
    }
  },
  // 获取当前权限
  async _getAuthority() {
    let _this = this
    return wx.showModal({
      title: '提示',
      content: '您当前没有登陆，是否去登录？',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../login/login',
          })
        } else if (res.cancel) {
          wx.switchTab({
            url: '../index/index',
          })
        }
      },
    })
  },
  // 点击展开全部
  _changeMin(e) {
    let type = e.currentTarget.dataset.id
    const {
      infoData: { fangyuan },
    } = this.data
    if (type == 1) {
      this.setData({ min: fangyuan.length, show: true })
    } else if (type == 2) {
      this.setData({ min: 2, show: false })
    }
  },
  // 获取详情
  async _getInfo({ id, uid }) {
    let res = await wx.requestAsync({
      url: 'index/info',
      data: {
        id,
        uid,
      },
    })
    let infoData = res.res
    this.setData({
      infoData,
    })
  },
  // 点击收藏//或取消
  async _changeColl(e) {
    let type = e.currentTarget.dataset.id
    let {
      infoData: { id, uid },
    } = this.data
    if (type == 1) {
      await wx.requestAsync({
        url: 'index/scqx',
        data: {
          id,
          uid,
        },
      })
      wx.showToast({
        title: '取消成功',
        icon: 'none',
        duration: 3000,
      })
      this.setData({ 'infoData.is_coll': 2 })
    } else if (type == 2) {
      await wx.requestAsync({
        url: 'index/scadd',
        data: {
          id,
          uid,
        },
      })
      wx.showToast({
        title: '收藏成功',
        icon: 'none',
        duration: 3000,
      })
      this.setData({ 'infoData.is_coll': 1 })
    }
  },
  //轮播数字切换
  changeImg(e) {
    let that = this
    that.activeIndex = e.detail.current
    this.setData({
      activeIndex: e.detail.current,
    }),
      console.log(that.activeIndex)
  },
  showLength() {
    return this.swiperList.length
  },
  //电话咨询
  bindTel: function (e) {
    const {
      infoData: { tel },
    } = this.data
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: tel, //仅为示例，并非真实的电话号码
    })
  },
  //点击放大图片
  previewImg: function (e) {
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    var imgurl = this.data.infoData.imgurl
    wx.previewImage({
      current: imgurl[index], //当前图片地址
      urls: imgurl, //所有要预览的图片的地址集合 数组形式
    })
  },
})
